﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.DAL;
using Matrix.MaKaYunShang.Model.Dto;
using Matrix.MaKaYunShang.Model.Enum;

namespace Matrix.MaKaYunShang.BLL
{
    public partial class ConsumerBLL : BaseBLL<ConsumerBLL>
    {
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Consumer model)
        {
            return ConsumerDAL.Instance.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Consumer model)
        {
            return ConsumerDAL.Instance.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            return ConsumerDAL.Instance.Delete(Id);
        }
        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return ConsumerDAL.Instance.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Consumer GetModel(int Id)
        {
            var ds = ConsumerDAL.Instance.GetModel(Id);
            return ds == null ? null : ServicesHelper.DataSetToIList<Consumer>(ds).ToList()[0];
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Consumer> GetModelList(string strWhere = null)
        {
            var ds = ConsumerDAL.Instance.GetList(strWhere);
            return ServicesHelper.DataSetToIList<Consumer>(ds).ToList();
        }
        #endregion

        #region 扩展方法
        /// <summary>
        /// 通过码云卡云中心微信用户公众号绑定记录ID寻找消费者记录
        /// </summary>
        /// <param name="mxWechatInfoUserId"></param>
        /// <returns></returns>
        public Consumer GetConsumerInfoByMxWechatInfoUserId(int mxWechatInfoUserId)
        {
            return ConsumerDAL.Instance.GetConsumerInfoByMxWechatInfoUserId(mxWechatInfoUserId);
        }

        /// <summary>
        ///     获得分页数据列表
        /// </summary>
        public TableDataViewDto<ViewConsumerDto> GetPagingList(List<MagModules> allowedModules, int draw, int pageIndex, int pageSize, string keyWord, string shopIds = null, bool isMag = false)
        {
            try
            {
                var totalCount = 0;
                var list = ConsumerDAL.Instance.GetPagingList(pageIndex, pageSize, keyWord, shopIds, ref totalCount, isMag);
                var shopList = new List<Shops>();
                if (list.Count > 0 && isMag)
                {
                    shopList = ShopsBLL.Instance.GetModelList();
                }
                var theReturnList = new List<ViewConsumerDto>();
                foreach (var o in list)
                {
                    #region 数据库实体与前台展现实体转换
                    var temp = new ViewConsumerDto();
                    temp.Id = o.Id;
                    temp.NickName = o.NickName;
                    temp.RealName = o.RealName;
                    temp.Mobile = o.Mobile;
                    temp.Email = o.Email;
                    temp.Sex = o.Sex == 1 ? "男" : "女";
                    temp.RegTime = o.RegTime.ToString();
                    temp.CouponCount = o.CouponCount;
                    if (isMag)
                    {
                        if (allowedModules.FindAll(t => t
                            .Action.Equals("ChangeShop") && t
                                .Controller.Equals("Consumer")).Count > 0)
                        {
                            temp.Action +=
                                "<a data-target='#ChangeShopModal' data-toggle='modal' class='btn btn-info' onclick='changeShop(" +
                                o.Id + ", 0);'>" + (o.ShopId > 0 ? "更改绑定店铺" : "绑定新店铺") + "</a> ";

                            if (o.ShopId > 0)
                            {
                                temp.Action += "<a class='btn btn-info' onclick=unbundShop(" + o.Id + ");>解绑店铺</a> ";
                            }
                            else
                            {
                                temp.Action +=
                                    "<a data-target='#ChangeShopModal' data-toggle='modal' class='btn btn-info' onclick='changeShop(" +
                                    o.Id + ", 1);'>设置默认访问店铺</a> ";
                            }
                        }
                        if (allowedModules.FindAll(t => t
                            .Action.Equals("List") && t
                                .Controller.Equals("UserAddress")).Count > 0)
                        {
                            temp.Action += " <a class='btn btn-default' href=\"/UserAddress/List/" + temp.Id +
                                           "\");>地址管理</a> ";
                        }
                        if (o.ShopId > 0)
                        {
                            var shop = shopList.Find(l => l.Id == o.ShopId);
                            if (shop != null)
                            {
                                temp.ShopName = shop.Name;
                            }
                        }
                        else
                        {
                            var lastConsumerShop = ConsumerShopBLL.Instance.GetLastConsumerShopModel(o.Id);
                            if (lastConsumerShop != null && lastConsumerShop.RecordType == (int)CommonEnum.ConsumerShopRecordType.Artificial)
                            {
                                var recordShop = ShopsBLL.Instance.GetModel(lastConsumerShop.ShopId);
                                if (recordShop != null)
                                {
                                    temp.ShopName = recordShop.Name + "<font style='color:blue'>（默认）</font>";
                                }
                            }
                        }
                    }
                    if (o.ShopAccountId > 0)
                    {
                        var mcAccount = McAccountsBLL.Instance.GetModel(o.ShopAccountId);
                        if (mcAccount != null)
                        {
                            temp.ShopAccountDisplayName = mcAccount.DisplayName;
                        }
                    }
                    #endregion

                    theReturnList.Add(temp);
                }
                var returnDataList = new TableDataViewDto<ViewConsumerDto>
                {
                    data = theReturnList,
                    draw = draw,
                    recordsFiltered = totalCount,
                    recordsTotal = totalCount
                };
                return returnDataList;
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetPagingList | Exception: " + ex);
                return null;
            }
        }
        #endregion
    }
}